package com.gc.web.controller.business;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Arrays;
import java.util.stream.Collectors;

import cn.hutool.core.collection.CollUtil;
import com.gc.business.domain.CommunityManage;
import com.gc.business.domain.ElevatorInfo;
import com.gc.business.service.IElevatorInfoService;
import com.gc.common.base.Result;
import com.gc.common.support.Convert;
import com.gc.common.utils.StringUtils;
import com.gc.common.utils.bean.BeanUtils;
import com.gc.framework.util.ShiroUtils;
import com.gc.system.service.ISysUserService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.gc.common.annotation.Log;
import com.gc.common.enums.BusinessType;
import com.gc.business.domain.AnnualPlan;
import com.gc.business.service.IAnnualPlanService;
import com.gc.framework.web.base.BaseController;
import com.gc.common.utils.poi.ExcelUtil;
import lombok.extern.slf4j.Slf4j;

/**
 * 电梯年检计划 控制层
 * 
 * @author hruiyan
 * @date 2020-12-01
 */
@Slf4j
@Controller
@RequestMapping("/business/annualPlan")
public class AnnualPlanController extends BaseController {
    private String prefix = "business/annualPlan";

    @Autowired
	private ISysUserService userService;
	
	private final IAnnualPlanService annualPlanService;

	private final IElevatorInfoService elevatorInfoService;

	@Autowired
	public AnnualPlanController(IAnnualPlanService annualPlanService,
								IElevatorInfoService elevatorInfoService
	) {
		this.annualPlanService = annualPlanService;
		this.elevatorInfoService = elevatorInfoService;
	}

	/**
	*  列表页
	*/
	@RequiresPermissions("business:annualPlan:view")
	@GetMapping
	public String annualPlan(){
	    return prefix + "/annualPlan";
	}

	/**
	 * 新增页
	 */
	@GetMapping("/add")
	public String add(){
		return prefix + "/add";
	}

	/**
	 * 编辑页
	 */
	@GetMapping("/edit/{planId}")
	public String edit(@PathVariable("planId") Long planId, ModelMap mmap){
		AnnualPlan annualPlan = annualPlanService.getById(planId);
		mmap.put("annualPlan", annualPlan);
		return prefix + "/edit";
	}
	
	/**
	 * 查询电梯年检计划列表
	 */
	@RequiresPermissions("business:annualPlan:list")
	@PostMapping("/list")
	@ResponseBody
	public Result list(AnnualPlan annualPlan){

		Long attrId = getAttrId();
		if(userService.ifCompanyRole(attrId)){
			annualPlan.setCompanyId(attrId);
			startPage();
			List<AnnualPlan> list = annualPlanService.selectAnnualPlanList(annualPlan);
			return Result.success(getDataTable(list));
		}else{
			return Result.success(getDataTable(new ArrayList<AnnualPlan>()));
		}
	}

	/**
	 * 一键添加年检计划
	 */
	@RequiresPermissions("business:annualPlan:add")
	@Log(title = "电梯年检计划", businessType = BusinessType.INSERT)
	@PostMapping("/add")
	@ResponseBody
	public Result addSave(){

		List<CommunityManage> communityList = userService.findCommunityByUserId(ShiroUtils.getUserId());
		if (CollUtil.isEmpty(communityList)){
			return Result.success();
		}
		List<Long> collect = communityList.stream().map(CommunityManage::getCommunityId).collect(Collectors.toList());
		List<ElevatorInfo> elevatorList = elevatorInfoService.list(new ElevatorInfo(),collect);

		if(elevatorList.size() == 0){return Result.failed("暂无电梯信息 ！、请先添加");}

		AnnualPlan annualPlan = null;
		for (ElevatorInfo elevatorInfo : elevatorList) {
			annualPlan = new AnnualPlan();
			annualPlan.setElevatorId(elevatorInfo.getElevatorId());
			AnnualPlan annualList = annualPlanService.selectByElevatorId(elevatorInfo.getElevatorId());
			if(StringUtils.isNotNull(annualList)){
				BeanUtils.copyBeanProp(annualList,elevatorInfo);
				annualList.setUpdateBy(ShiroUtils.getLoginName());
				annualList.setUpdateTime(new Date());
				annualList.setAnnualDate(elevatorInfo.getNextAnnualDate());
				annualPlanService.updateById(annualList);
			}else{
				BeanUtils.copyBeanProp(annualPlan,elevatorInfo);
				annualPlan.setCreateBy(ShiroUtils.getLoginName());
				annualPlan.setCreateTime(new Date());
				//保存下次年检日期作为年检计划的年检日期
				annualPlan.setAnnualDate(elevatorInfo.getNextAnnualDate());
				annualPlanService.save(annualPlan);
			}
		}
		return Result.success();
	}

	/**
	 * 修改保存电梯年检计划
	 */
	@RequiresPermissions("business:annualPlan:edit")
	@Log(title = "电梯年检计划", businessType = BusinessType.UPDATE)
	@PostMapping("/edit")
	@ResponseBody
	public Result editSave(AnnualPlan annualPlan){
		annualPlanService.updateById(annualPlan);
		return Result.success();
	}
	
	/**
	 * 删除电梯年检计划
	 */
	@RequiresPermissions("business:annualPlan:remove")
	@Log(title = "电梯年检计划", businessType = BusinessType.DELETE)
	@PostMapping( "/remove")
	@ResponseBody
	public Result remove(String ids){
		Long[] longIds = Convert.toLongArray(ids);
		annualPlanService.removeByIds(Arrays.asList(longIds));
		return Result.success();
	}

	/**
	 * 导出电梯年检计划列表
	 */
	@RequiresPermissions("business:annualPlan:export")
	@PostMapping("/export")
	@ResponseBody
	public Result export(AnnualPlan annualPlan){
		List<AnnualPlan> list = annualPlanService.list(annualPlan);
		ExcelUtil<AnnualPlan> util = new ExcelUtil<>(AnnualPlan.class);
		util.exportExcel(list, "annualPlan");
		return Result.success();
	}
}
